/*
 ============================================================================
 Name		: VibraMassagerContainer.cpp
 Author	  : 
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CVibraMassagerContainer implementation
 ============================================================================
 */

#include <coemain.h>
#include "VibraMassagerContainer.h"
#include <LemonAssistant.mbg>
#include "LMSvgUtil.h"
#include "MacroUtil.h"
#include "LemonAssistantAppUi.h"
#include "CommonUtils.h"

CVibraMassagerContainer::CVibraMassagerContainer()
:iVibraState(EVibraStateUnKnown)
	{
	iState = EVibraMassagerOff;
	// No implementation required
	}

CVibraMassagerContainer::~CVibraMassagerContainer()
	{
	SAFE_DELETE(iImgLight);
	SAFE_DELETE(iImgLightGray);
	SAFE_DELETE(iImgLightMask);

	delete iVibra;
	}

CVibraMassagerContainer* CVibraMassagerContainer::NewLC(const TRect& aRect)
	{
	CVibraMassagerContainer* self = new (ELeave) CVibraMassagerContainer();
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

CVibraMassagerContainer* CVibraMassagerContainer::NewL(const TRect& aRect)
	{
	CVibraMassagerContainer* self = CVibraMassagerContainer::NewLC(aRect);
	CleanupStack::Pop(); // self;
	return self;
	}

void CVibraMassagerContainer::ConstructL(const TRect& aRect)
	{
	CreateWindowL();
	SetRect(aRect);

	iVibra = CHWRMVibra::NewL(this);

	iImgLight = LMSvgUtil::GetImageFromResourceL(
			EMbmLemonassistantVibramassager);
//	iImgLightGray = LMSvgUtil::GetImageFromResourceL(
//			EMbmLemonassistantVibramassagergray);
	iImgLightMask = LMSvgUtil::GetImageFromResourceL(
			EMbmLemonassistantVibramassagermask);

	ActivateL();
	
	iState = EVibraMassagerOn;
	VibraOn();
	}

// ---------------------------------------------------------
// CVibraMassagerContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CVibraMassagerContainer::CountComponentControls() const
	{
	return 0; // return nbr of controls inside this container
	}

// ---------------------------------------------------------
// CVibraMassagerContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CVibraMassagerContainer::ComponentControl(TInt aIndex) const
	{
	switch (aIndex)
		{
		default:
			return NULL;
		}
	}

// ---------------------------------------------------------
// CVibraMassagerContainer::HandleControlEventL(
//	 CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CVibraMassagerContainer::HandleControlEventL(CCoeControl* /*aControl*/,
		TCoeEvent /*aEventType*/)
	{
	// TODO: Add your control event handler code here
	}

//------------------------------------------------------------------
//CVibraMassagerContainer::OfferKeyEventL(
// const TKeyEvent& aKeyEvent,	TEventCode aType)
//-------------------------------------------------------------------
//
TKeyResponse CVibraMassagerContainer::OfferKeyEventL(
		const TKeyEvent& /*aKeyEvent*/, TEventCode /*aType*/)
	{
	// See if we have a selection
//	switch (aType)
//		{
//		case EEventKey:
//			switch (aKeyEvent.iCode)
//				{
//				case EKeyDevice3://ok
//					Vibra();
//					return EKeyWasConsumed;
//				}
//			break;
//		}
	return EKeyWasNotConsumed;
	}
// -----------------------------------------------------------------------------
// CVibraMassagerContainer::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CVibraMassagerContainer::Draw(const TRect& /*aRect*/) const
	{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc();
	TRect drawRect(Rect());
	gc.Clear(drawRect);

	if (iState == EVibraMassagerOn)
		{
		if (iImgLight)
			{
			TRect rect = iImgLight->SizeInPixels();
			TPoint point;
			point.iX = (Rect().Width() - rect.Width()) >> 1;
			point.iY = (Rect().Height() - rect.Height()) >> 1;
			gc.BitBltMasked(point, iImgLight, rect, iImgLightMask, ETrue);
			//gc.BitBlt(point,iImgLightMask);
			}
		}
	else
		{
		if (iImgLightGray)
			{
			TRect rect = iImgLightGray->SizeInPixels();
			TPoint point;
			point.iX = (Rect().Width() - iImgLightGray->SizeInPixels().iWidth)
					>> 1;
			point.iY
					= (Rect().Height() - iImgLightGray->SizeInPixels().iHeight)
							>> 1;
			gc.BitBltMasked(point, iImgLightGray, rect, iImgLightMask, ETrue);
			//gc.BitBlt(point,iImgLightGray);
			}
		}

	//add your code here...
	}

// -----------------------------------------------------------------------------
// CVibraMassagerContainer::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CVibraMassagerContainer::SizeChanged()
	{
	DrawNow();
	}

void CVibraMassagerContainer::VibraModeChanged(
		CHWRMVibra::TVibraModeState aState)
	{
	switch (aState)
		{
		case CHWRMVibra::EVibraModeUnknown:
			{
			// EVibraModeUnknown do something...  
			break;
			}
		case CHWRMVibra::EVibraModeON:
			{
			// EVibraModeON do something...  
			break;
			}
		case CHWRMVibra::EVibraModeOFF:
			{
			// EVibraModeOFF do something...  
			break;
			}
		default:
			{
			// Vibra mode undefined do something...  
			break;
			}
		}
	}

void CVibraMassagerContainer::VibraStatusChanged(
		CHWRMVibra::TVibraStatus aStatus)
	{
	switch (aStatus)
		{
		case CHWRMVibra::EVibraStatusUnknown:
			{
			// EVibraStatusUnknown do something...            	
			break;
			}
		case CHWRMVibra::EVibraStatusNotAllowed:
			{
			// EVibraStatusUnknown do something...
			break;
			}
		case CHWRMVibra::EVibraStatusStopped:
			{
			// EVibraStatusUnknown do something...
			if(iVibraState != EVibraStateUnKnown)
				{
				VibraOn();
				}
			break;
			}
		case CHWRMVibra::EVibraStatusOn:
			{
			// EVibraStatusUnknown do something...
			iVibraState = EVibraStateRunning;
			break;
			}
		default:
			{
			// Vibra status undefined do something...           
			break;
			}
		}
	}

void CVibraMassagerContainer::Vibra()
	{
	if (iState == EVibraMassagerOn)
		{
		VibraOff();
		iState = EVibraMassagerOff;
		}
	else if (iState == EVibraMassagerOff)
		{
		VibraOn();
		iState = EVibraMassagerOn;
		}
	iVibraState = EVibraStateUnKnown;
	DrawNow();
	}

void CVibraMassagerContainer::VibraOn()
	{
	TRAPD(err,iVibra->StartVibraL( 0 );)

	if (err == KErrNone)
		{

		}
	else
		{

		}
	}
void CVibraMassagerContainer::VibraOff()
	{
	TRAPD(err,iVibra->StopVibraL();)

	if (err == KErrNone)
		{

		}
	else
		{

		}
	}

void CVibraMassagerContainer::HandlePointerEventL(const TPointerEvent& aEvent)
	{
	/*
	//	if ( AknLayoutUtils::PenEnabled() )
	if (OKCUI()->CheckPenEnabled())
		{
		CCoeControl::HandlePointerEventL(aEvent);
		
		if (aEvent.iType == 1 && CCommonUtils::IsPointInRect(
				aEvent.iPosition, Rect()))
			{
			Vibra();
			DrawNow();
			}
		}
		*/
	}

void CVibraMassagerContainer::HandleResourceChange( TInt aType )
    {
    CCoeControl::HandleResourceChange( aType );
    if  ( aType == KEikDynamicLayoutVariantSwitch )
    	{
        TRect rect;
        AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
        SetRect(rect);
        }
    }
// End of File
